﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Projektet.Model.Combat;

namespace Projektet.Model
{
    public enum UnitState 
    {
        Moving,
        Fighting,
        StandingStill
    }

    public abstract class CombatUnit
    {
        public PlayerType _playerType;
        public UnitState _unitState = UnitState.Moving;
        public float _health;
        public float _damage;
        public float _noDamage = 0f;
        public float _attackCD;
        public float _attackInterval;
        public float _moveSpeed;
        public Rectangle _unitRect;
        public Vector2 _unitPosition;
        public int _width;
        public int _height;

        public void LoadContent(ContentManager contManager) { }

        public virtual void Update(GameTime gameTime) 
        {
            if(_unitState == UnitState.Moving)
            {
                _unitPosition.X += _moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                _unitRect = new Rectangle((int)_unitPosition.X, (int)_unitPosition.Y, _width, _height);                
            }
            else if(_unitState == UnitState.Fighting)
            {
                _attackCD += gameTime.ElapsedGameTime.Milliseconds;
            }
            //Console.WriteLine(_unitPosition.X);
        }
        
        public Attack DealDamage() 
        {
            if(_attackCD > _attackInterval)
            {
                _attackCD = 0;
                return new Attack(_damage, this);
            }
            else
            {
                return new Attack(_noDamage, this);
            }            
        }

        public virtual void TakeDamage(Combat.Attack attack) 
        {
            _health -= attack.Power;
        }
        public void TakeDamage(float damage) 
        {
            Console.WriteLine(_health);
            _health -= damage;
        }

        public virtual bool IsDead() 
        {
            if(_health <= 0)
            {
                //Console.WriteLine("Jag dog!");
                return true;
            }
            else 
            {
                return false;
            }
        }

        public virtual bool IsInContact(Rectangle targerRect)
        {
            if(_unitRect.Intersects(targerRect))
            {
                _unitState = UnitState.Fighting;
                return true;
            }
            else 
            {
                //_unitState = UnitState.Moving;
                return false;
            }            
        }
    }
}
